package info.cs52.fes.lp;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PrologLanguageParser extends LanguageParser{
	public static final int YN = 0;
	public static final int STRING = 1;
	public static final int LIST = 2;
	public static final int NUMBER = 3;
	
	private int answerType = YN;
	private float cf = 0.99f;
	
	@Override
	public void parse(){
		if (answerType==YN){
			NatureLanguageParser nlp = new NatureLanguageParser();
			nlp.setOriginalPhrase(originalPhrase);
			nlp.setDefaultPattern(defaultPattern);
			nlp.parse();
			if (nlp.getTranslatedPhrase().compareToIgnoreCase("no")==0){
				cf = -0.99f;
			} else {
				cf = 0.99f;
			}
			translatedPhrase = "yes";
		} else if (answerType==STRING){
			NatureLanguageParser nlp = new NatureLanguageParser();
			nlp.setOriginalPhrase(originalPhrase);
			nlp.setDefaultPattern(defaultPattern);
			nlp.parse();
			translatedPhrase = nlp.getTranslatedPhrase();
			cf = 0.99f;
		} else if (answerType == NUMBER){
			String pattern = "\\d+";
			Pattern p = Pattern.compile(pattern);
			Matcher m = p.matcher(originalPhrase);
			m.find();
			translatedPhrase = m.group();	
			cf = 0.99f;
		} else if (answerType == LIST){
			StringBuffer buf = new StringBuffer("[");
			String[] list = originalPhrase.split(",");
			for (String str : list) {
				if (!str.isEmpty()){
					NatureLanguageParser nlp = new NatureLanguageParser();
					nlp.setOriginalPhrase(str);
					nlp.setDefaultPattern(defaultPattern);
					nlp.parse();
					if (nlp.getErrorCode()==0){
						buf.append(nlp.getTranslatedPhrase());
						buf.append(",");
					}
				}				
			}
			buf.deleteCharAt(buf.length()-1);
			buf.append("]");
			translatedPhrase = buf.toString();
			cf = 0.99f;
		} else {
			translatedPhrase = "er_hc";
		}
	}

	public float getCf() {
		return cf;
	}

	public int getAnswerType() {
		return answerType;
	}

	public void setAnswerType(int answerType) {
		this.answerType = answerType;
	}
	
	
}
